<?php


include 'helpers.php';
class Predis {
    private $redis=null;

    public function __construct()
    {
       return  $this->getInstance();
    }

    public function getInstance(){
        if(is_null($this->redis)){
            $this->redis = new \redis();
           $result = $this->redis->connect('127.0.0.1',6379);
           if(!$result){
               die('连接失败');
           }
        }
        return $this->redis;
    }

    public function set($key,$value){
        return $this->redis->set($key,$value);
    }

    public function setnx($key,$value){
        return $this->redis->setnx($key,$value);
    }

    //判断key是否存在
    public function existss($key){
        return $this->redis->setnx($key);
    }

    //自增1
    public function incr($key){
        return $this->redis->incr($key);
    }

    //自增 $value
    public function incrBy($key,$value){
        return $this->redis->incrBy($key,$value);
    }
    //自减1
    public function decr($key){
        return $this->redis->decr($key);
    }

    //自减 $value
    public function decrBy($key,$value){
        return $this->redis->decrBy($key,$value);
    }

    //获取多个key的值，没有返回空
    public function getMultiple($key_ar){
        return $this->redis->getMultiple($key_ar);
    }

    //列表 从左向列表塞数据
    public function lpush($key,$value){
        return $this->redis->lpush($key,$value);
    }

    //列表 从右向列表塞数据
    public function rpush($key,$value){
        return $this->redis->rpush($key,$value);
    }

    //列表 从右向列表塞数据
    public function lpop($key,$value){
        return $this->redis->rpush($key,$value);
    }
    //调用一个不存在的方法
    public function __call($name, $value)
    {
        return  $this->redis->$name(...$value);
    }


}

$ob= new Predis();

p($ob->set('aaa',55));
p($ob->set('aaa',595));
p($ob->setnx('aaa',595));
p($ob->exists('b'));
p($ob->incr('c'));
p($ob->incrBy('baa',2));
p($ob->decr('c'));
p($ob->decrBy('baa',3));
p($ob->getMultiple([1,'aaa']));
p($ob->lpush('list1','112'));
p($ob->lpush('list1','112'));
p($ob->rpush('list1',563));

p($ob);die;